package com.blue.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * IO常用工具包
 */
public class CustomIOUtils {

    /**
     * 将输入流转换成字节流
     *
     * @param input
     * @return
     * @throws Exception
     */
    public static byte[] toBytes(InputStream input) throws Exception {
        byte[] data = null;
        try {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int read = 0;
            while ((read = input.read(b)) > 0) {
                byteOut.write(b, 0, read);
            }
            data = byteOut.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            input.close();
        }
        return data;
    }

    /**
     * 将文件读取为一个字符串
     *
     * @param file
     * @return
     * @throws Exception
     */
    public static String toString(File file) throws Exception {
        return toString(new FileInputStream(file));
    }

    /**
     * 将输入流转换为一个串
     *
     * @param input
     * @return
     * @throws Exception
     */
    public static String toString(InputStream input) throws Exception {
        return toStringWithLineBreak(input, null);
    }

    /**
     * 以指定编码格式将输入流按行置入一个List<String>
     *
     * @param input
     * @return
     * @throws Exception
     */
    public static List<String> toLines(InputStream input, String encoding) throws Exception {
        InputStreamReader insreader = new InputStreamReader(input, encoding);
        BufferedReader bin = new BufferedReader(insreader);
        List<String> lines = new ArrayList<String>();
        String line;
        while ((line = bin.readLine()) != null) {
            lines.add(line);
        }
        bin.close();
        insreader.close();
        return lines;
    }

    /**
     * 以GBK格式将输入流按行置入一个List<String>
     *
     * @param input
     * @return
     * @throws Exception
     */
    public static List<String> toLines(InputStream input) throws Exception {
        return toLines(input, "GBK");
    }

    /**
     * 转换为每行补充指定换行符(例如："/n"，"</br>
     * ")
     *
     * @param input
     * @param lineBreak
     * @return
     * @throws Exception
     */
    public static String toStringWithLineBreak(InputStream input, String lineBreak) throws Exception {
        List<String> lines = toLines(input);
        StringBuilder sb = new StringBuilder(20480);
        for (String line : lines) {
            sb.append(line);
            if (lineBreak != null) {
                sb.append(lineBreak);
            }
        }
        return sb.toString();
    }

    /**
     * 根据行号读取内容，转换为每行补充指定换行符,并且返回行号(例如："/n"，"</br>
     * ")
     *
     * @param input
     * @param finishString
     * @param lineNum
     * @return
     * @throws Exception
     */
    public static Map<String, Object> toStringWithLineBreakAndLineNum(InputStream input,String finishString, long lineNum)
            throws Exception {
        List<String> lines = toLines(input);
        StringBuilder sb = new StringBuilder(20480);
        int currentLineNum = 0;
        boolean closeFlag = false;
        System.out.println("\n===================开始本次读取,从第"+lineNum+"行开始读===================\n");
        for (String line : lines) {
            if (currentLineNum > lineNum) {
                sb.append(line + "\n");
                if (line.indexOf(finishString) > -1) {
                    closeFlag = true;
                    System.out.println("closeFlag====="+closeFlag);
                    break;
                }
            }
            currentLineNum++;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("lineString", sb.toString());
        map.put("lineNum", currentLineNum-1);
        map.put("flag", closeFlag);
        
        /*if(sb.toString().contains("error:") || sb.toString().contains("err:") || sb.toString().contains("Error:")){
            map.put("errorFlag",true);
        }*/

        if(sb.toString().contains("error:") || sb.toString().contains("Error:")){
            map.put("errorFlag",true);
        }
        System.out.println(sb.toString());
        System.out.println("\n===================结束本次读取,当前读到了第"+(currentLineNum-1)+"行===================\n");
        return map;
    }

    
    /**
     * 根据行号读取内容，转换为每行补充指定换行符,并且返回行号(例如："/n"，"</br>
     * ")
     *
     * @param input
     * @param finishString
     * @param lineNum
     * @return
     * @throws Exception
     */
    public static Map<String, Object> toStringWithLineBreakAndLineNumVerify(InputStream input,String finishString, long lineNum)
            throws Exception {
        List<String> lines = toLines(input);
        StringBuilder sb = new StringBuilder(20480);
        int currentLineNum = 0;
        boolean closeFlag = false;
        System.out.println("\n===================开始本次读取,从第"+lineNum+"行开始读===================\n");
        for (String line : lines) {
            if (currentLineNum > lineNum) {
                sb.append(line + "\n");
                if (line.indexOf(finishString) > -1) {
                    closeFlag = true;
                    System.out.println("closeFlag====="+closeFlag);
                    break;
                }
            }
            currentLineNum++;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("lineString", sb.toString());
        map.put("lineNum", currentLineNum-1);
        map.put("flag", closeFlag);
        
        if(sb.toString().contains("error:") || sb.toString().contains("Error:")){
            map.put("errorFlag",true);
        }
        System.out.println(sb.toString());
        System.out.println("\n===================结束本次读取,当前读到了第"+(currentLineNum-1)+"行===================\n");
        return map;
    }
    
    /**
     * 将字符串转出到指定文件
     *
     * @param targetFile
     * @param content
     */
    public static void toFile(String content, File targetFile) throws IOException {
        File parent = targetFile.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        if (!targetFile.exists()) {
            targetFile.createNewFile();
        }
        PrintWriter out = null;
        try {
            out = new PrintWriter(new FileWriter(targetFile));
            out.print(content);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * 将一组文件打zip包
     *
     * @param srcFiles
     * @param targetFileName
     * @throws IOException
     */
    public static void filesToZip(List<File> srcFiles, String targetFileName) throws IOException {
        String fileOutName = targetFileName + ".zip";
        byte[] buf = new byte[1024];
        FileInputStream in = null;
        FileOutputStream fos = null;
        ZipOutputStream out = null;
        try {
            fos = new FileOutputStream(fileOutName);
            out = new ZipOutputStream(fos);
            for (File file : srcFiles) {
                in = new FileInputStream(file);
                out.putNextEntry(new ZipEntry(file.getName()));
                int len;
                while ((len = in.read(buf)) != -1) {
                    out.write(buf, 0, len);
                }
                if (in != null) {
                    in.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
            if (fos != null) {
                out.closeEntry();
                out.close();
                fos.close();
            }
        }
    }

    public static void main(String[] args) {
        try {
            File doc1 = new File("E://workspace//test//doc//1272531757100_1.doc");
            CustomIOUtils.toString(new FileInputStream(doc1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}